// pages/square/square.js
Page({

  /**
   * 页面的初始数据
   */
  
  data: {
    firco: "#000000",
    secco: "#979797",
    showdata:{}
    // list: [{
    //     face_url: "http://happygoing.oss-cn-beijing.aliyuncs.com/img/102356825.jpg",
    //     username: "张起灵",
    //     send_timestamp: "2020-8-17 00:00",
    //     centent: "湖边，走走聊聊",
    //     total_likes: 2,
    //   },
    //   {
    //     face_url: "http://happygoing.oss-cn-beijing.aliyuncs.com/img/102356826.jpg",
    //     username: "吴邪",
    //     send_timestamp: "2020-11-1 13:14",
    //     centent: "陪伴是最好的礼物~",
    //     total_likes: 6,
    //   },
    //   {
    //     face_url: "http://happygoing.oss-cn-beijing.aliyuncs.com/img/th",
    //     username: "王月半",
    //     send_timestamp: "2020-11-6 15:14",
    //     centent: "只羡鸳鸯不羡仙~",
    //     total_likes: 6,
    //   }
    // ]
  },

  like:function(e){
    //与服务器交互
    wx.request({
      url: getApp().globalData.server+'/Message/do_like',
      data: {
        message_id:e.target.id,
        user_id:getApp().globalData.user.user_id,
      },
      method:"POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded' 
      },
      success (res) {
        console.log(res.data)
        if(res.data.error_code!=0)
        {
          wx.showModal({
            title: '提示！',
            content: '错误'+res.data.msg,
            showCancel: false,
            success(res) {}
          })
        }
        else if(res.data.error_code==0)
        {
          wx.showModal({
            title: '提示！',
            content: '点赞成功',
            showCancel: false,
            success(res) {},
            complete:function(res){
              wx.reLaunch({   //正好实现了树洞列表的重新加载
                url: '/pages/square/square',
              })
            }
          })
        }
      },
      fail:function(res)
      {
        wx.showModal({
          title: '提示！',
          content: 'Fail Connect!',
          showCancel: false,
          success(res) {}
        })
      },
      complete:function(res){
      }
    })
  },

  first_select: function() {
    // wx.redirectTo({
    //   url: '../square/square'
    // })
  },

  second_select: function() {
    wx.navigateTo({
      url: '../commit/commit'
    })
  },

  third_select: function() {
    wx.reLaunch({
      url: '/pages/mine/mine'
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that=this

    wx.showLoading({
      title: '加载中',
    })
    //与服务器交互
    wx.request({
      url: getApp().globalData.server+'/Message/get_all_messages',
      data: {
      },
      method:"POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded' 
      },
      success (res) {
        console.log(res.data)
        if(res.data.error_code!=0)
        {
          wx.showModal({
            title: '提示！',
            content: '其他错误',
            showCancel: false,
            success(res) {}
          })
        }
        else if(res.data.error_code==0)
        {
          //简单的=不会引起视图层的数据赋值刷新
          //需要使用setData方法
          that.setData({
            showdata:res.data.data
          })
          that.data.showdata=res.data.data,
          console.log(that.data.showdata)
        }
      },
      fail:function(res)
      {
        wx.showModal({
          title: '提示！',
          content: 'Fail Connect!',
          showCancel: false,
          success(res) {}
        })
      },
      complete:function(res){
        wx.hideLoading()
      }
    })

    setTimeout(function () {
      wx.hideLoading()
    }, 2000)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})